// var webpackMerge = require('webpack-merge')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var webpack = require('webpack')
var CopyWebpackPlugin = require('copy-webpack-plugin')
var TypedocWebpackPlugin = require('typedoc-webpack-plugin')
var commonsPlugin = new webpack.optimize.CommonsChunkPlugin('common.js')
var HtmlWebpackPlugin = require('html-webpack-plugin')

// var commonConfig = require('./webpack.common.js')
var utils = require('./utils')
var buildFor = 'demo'
const ENV = process.env.ENV = process.env.NODE_ENV = 'development'

module.exports = function () {
  // isProd = options.env === 'production'
  return {
    cache: true,

    resolve: {
      extensions: ['.js', '.json', '.ts']
    },

    module: {
      loaders: [{
        test: /\.ts$/,
        include: /node_modules\/wit-core/ | /src/,
        exclude: /node_modules/,
        loaders: [
          // '@angularclass/hmr-loader?pretty=true&prod=true',// + !isProd + '&prod=' + isProd,
          'awesome-typescript-loader',
          'angular2-template-loader',
          'angular2-router-loader'
        // 'angular2-router-loader?aot=true&genDir=src/compiled/src'
        ],
        exclude: [/\.(spec|e2e)\.ts$/]
      },
        {
          test: /\.html$/,
          loader: 'raw-loader'
        },

        {
          test: /\.css$/,
          include: utils.root(buildFor, 'src'),
          exclude: utils.root(buildFor, 'src', 'assets'),
          loader: 'raw' // raw-loader只针对.css文件
        },

        { test: /\.scss$/, exclude: utils.root(buildFor, 'src', 'assets'), loaders: ['to-string-loader', 'css-loader', 'sass-loader'] },
        { test: /\.scss$/, include: utils.root(buildFor, 'src', 'assets'), exclude: /\.useable\.scss$/, loader: ExtractTextPlugin.extract({ fallbackLoader: 'style-loader', loader: 'css!sass' }) },
        // { test: /\.useable\.scss$/, loader: "style/useable!css!sass" },

        { test: /\.(woff|woff2|ttf|eot|svg|map)$/, loader: 'file-loader?name=/assets/[name].[ext]' },
        { test: /\.(png|jpg|gif)$/, loader: 'url-loader?limit=8192&name=/assets/[name].[ext]' },
        { test: /\.json$/, include: utils.root('src'), loader: 'json-loader' }
      ]
    },

    entry: entries(utils.root(buildFor) + '/src/*'),
    devtool: 'cheap-module-source-map', // for  debug ts file,'eval'
    devServer: {
      historyApiFallback: true,
      stats: 'minimal'
    },
    output: {
      path: utils.root(buildFor, 'dist'),
      publicPath: '', // relative path  <>
      filename: 'js/[name].bundle.js',
      chunkFilename: 'js/[id].chunk.bundle.js',
    //    library: 'lib'
    },

    plugins: [
      new webpack.BannerPlugin('Owned by WIT'),

      // commonsPlugin,

      new webpack.optimize.CommonsChunkPlugin({ // common file
        name: ['ZZpolyfills', 'XXapp'].reverse()
      }),

      new HtmlWebpackPlugin({
        template: './' + buildFor + '/src/index.html'
      }),

      new CopyWebpackPlugin([{
        from: './' + buildFor + '/src/lib/lib.js',
        to: 'js/lib.js',
        force: true
      },
        {
          from: './' + buildFor + '/src/app.config.js',
          force: true
        },
        {
          from: './' + buildFor + '/src/config',
          to: 'config',
          force: true
        }
      ]),

      new ExtractTextPlugin('assets/[name].css'),

      new webpack.ContextReplacementPlugin(
        /angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
        utils.root(buildFor, 'src'), {}
      ),

      new webpack.DllReferencePlugin({
        context: __dirname,
        manifest: require('../' + buildFor + '/src/lib/manifest.json')
      }),

    // new TypedocWebpackPlugin({
    //     name: 'WIT',
    //     mode: 'file',
    //     includeDeclarations: false,
    //     ignoreCompilerErrors: true,
    // })
    ]
  }
}

function entries (globPath) {
  var entries = {
    'ZZpolyfills': './' + buildFor + '/src/polyfills.ts',
    'XXapp': './' + buildFor + '/src/main.ts',
    'ZZstyle': './' + buildFor + '/src/assets/lib.scss',
    'boot': './' + buildFor + '/src/assets/boot.scss'

  }
  // utils.getEntries(globPath, entries)
  return entries
}
